package view;

import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import model.Ressource;
import model.Uebung;

/**
 *
 */
public class UebungListView extends JScrollPane implements Observer {
    private ArrayList<JCheckBox> checkboxes;
    
    public UebungListView(){
        this.checkboxes = new ArrayList<JCheckBox>();
    }
    
    public void update(Observable o, Object arg) {
        List<Uebung> uebungen = (List<Uebung>) arg;
        
        JPanel panel = new JPanel();
        BoxLayout layout = new BoxLayout(panel, BoxLayout.Y_AXIS);
        panel.setLayout(layout);
        
        panel.add(Box.createRigidArea(new Dimension(1, 10)));
        
        for(Uebung uebung : uebungen) {
            JPanel uebpanel = new JPanel();
            uebpanel.setLayout(new BoxLayout(uebpanel, BoxLayout.Y_AXIS));
            
            JLabel headline = new JLabel(uebung.getNumber() + ". Uebungsaufgabe");
            headline.setFont(new Font(headline.getFont().getFamily(), Font.PLAIN, 15));
            uebpanel.add(headline);
            
            JCheckBox box = new JCheckBox(uebung.getLabel());
            box.setName(uebung.getUrl());
            this.getCheckboxes().add(box);
            uebpanel.add(box);
            
            for(Ressource ressource : (List<Ressource>) uebung.getRessources()) {
                String label = ressource.getLabel();
                
                if(!ressource.getName().equals("")) {
                    label = ressource.getName() + " (" + ressource.getLabel() + ")";
                }
                
                box = new JCheckBox(label);
                box.setName(ressource.getUrl());
                this.getCheckboxes().add(box);
                uebpanel.add(box);            
            }
            
            uebpanel.setAlignmentX((float)0.1);
            
            uebpanel.add(Box.createRigidArea(new Dimension(1, 20)));
            
            panel.add(uebpanel);
        }
        
        this.getViewport().add(panel);
        this.revalidate();
    }

    /**
     * @return the checkboxes
     */
    public ArrayList<JCheckBox> getCheckboxes() {
        return this.checkboxes;
    }
    
}


